import {/*createAsyncThunk,*/ createSlice} from "@reduxjs/toolkit";
import {getType} from "../../api/type";

// export const getTypeList = createAsyncThunk(
//     'type/getTypeList',
//     async () => {
//         const response = await getType()
//         return response.data.data
//     }
// )


const typeStore = createSlice({
    name: 'type',
    initialState: {
        // 存储所有的类型
        typeList: [],
        issueTypeId: 'all',
        bookTypeId: 'all'
    },
    reducers: {
        setTypeList(state, action) {
            state.typeList = action.payload
        },
        setIssueTypeId(state, action) {
            state.issueTypeId = action.payload
        },
        setBookTypeId(state, action) {
            state.bookTypeId = action.payload
        }
    },
    // 处理异步的 reducer
    // extraReducers: (builder) => {
    //     builder.addCase(getTypeList.fulfilled, (state, action) => {
    //         state.typeList = action.payload
    //     })
    // }
})
export const {setTypeList,setBookTypeId,setIssueTypeId} = typeStore.actions
export const getTypeList = () => {
    return async (dispatch) => {
        const response = await getType()
        dispatch(setTypeList(response.data.data))
    }
}
export default typeStore.reducer